#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>

#define KEY1 24
#define KEY2 25
#define KEY3 0
#define KEY4 1

int key_init(void) 
{
	pinMode(KEY1,INPUT);  	
    pinMode(KEY2,INPUT); //配置GPIO.25为输入模式
    pinMode(KEY3,INPUT); //配置GPIO.0为输入模式
    pinMode(KEY4,INPUT); //配置GPIO.1为输入模式
}
int key_scan(int mode);

int main(int argc, char *argv[])
{
    if(wiringPiSetup() == -1) //初始化必须写在最前边
    {
        printf("setup wiringPi failed !");
        return 1;
    }

    printf("Hello World!\n");
    
	key_init();
    while(1)		//进入主逻辑循环
    {
        int keyValue = key_scan(0);
        if(keyValue > 0)
        {
            printf("keyV: %d\n", keyValue);
        }
    }
    return 0;
}

int key_scan(int mode)
{
    static int key_up = 1;	//按键按松开标志
    if(mode){
            key_up = 1;         //支持连按
    }
    if(key_up && (digitalRead(KEY1) == 0)||(digitalRead(KEY2) == 0)
			||(digitalRead(KEY3) == 0)||(digitalRead(KEY4) == 0))
    {
            delay(50);          //去抖动
            key_up = 0;

            if(digitalRead(KEY1) == 0)   	 return 1;
            else if(digitalRead(KEY2) == 0)   return 2;
            else if(digitalRead(KEY3) == 0)   return 3;
            else if(digitalRead(KEY4) == 0)   return 4;

    }else if((digitalRead(KEY1) == 1) && (digitalRead(KEY2) == 1)
			&&(digitalRead(KEY3) == 1) && (digitalRead(KEY4) == 1))
    {
            key_up = 1;
    }
    return 0;
}






